package com.zxl.live.screen.screen.ui.helper.window;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/23.
 */

public class BaseWindowHelper {

    private List<View> mCache;

    private WindowManager mWindowManager;

    public BaseWindowHelper(Context context) {
        mCache = new ArrayList<>();
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    }

    public boolean addView(View view, WindowManager.LayoutParams params) {
        try {
            if (mCache.contains(view)) {
                mWindowManager.updateViewLayout(view, params);
            } else {
                mWindowManager.addView(view, params);
                mCache.add(view);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void removeView(View view) {
        try {
            if (mCache.contains(view)) {
                mWindowManager.removeView(view);
                mCache.remove(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void removeAllView() {
        for (View view : mCache) {
            try {
                mWindowManager.removeView(view);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mCache.clear();
    }

}
